package com.example.demo.config;

import java.io.File;

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.example.demo.util.PropertiesUtil;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class ImageConfig implements WebMvcConfigurer{
	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		File classpath = null;
		try {
			classpath = new File(ResourceUtils.getURL("classpath:").getPath());
			log.info("classpath的路径：{}",classpath);
		} catch (Exception e) {
			log.error("获取classpath路径失败",e);
			return;
		}
		
		
		String imageprefix = PropertiesUtil.getProperty("imageprefix");
		
		String imagepath = PropertiesUtil.getProperty("imagepath");
		
		String imageParentDir = imagepath.substring(imagepath.lastIndexOf(File.separator)+1, imagepath.length());
		
		String imgpath = classpath.getParentFile().getParentFile().getParent()+File.separator+imageParentDir+File.separator;
		
		log.info("图片存放的位置:{}",imgpath);
		
		registry.addResourceHandler("/"+imageprefix+"/**").addResourceLocations(imgpath);
		
		WebMvcConfigurer.super.addResourceHandlers(registry);
	}
}
